package cn.tangsihai.pay.service.impl;

import cn.tangsihai.pay.domain.PayAlipayInfo;
import cn.tangsihai.pay.mapper.PayAlipayInfoMapper;
import cn.tangsihai.pay.service.IPayAlipayInfoService;
import com.alipay.easysdk.kernel.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class PayAlipayInfoServiceImpl implements IPayAlipayInfoService {
    @Autowired
    private PayAlipayInfoMapper payAlipayInfoMapper;

    /**
     * 设置支付宝应用参数
     * @return
     */
    @Override
    @Transactional
    public Config getOptions() {
        PayAlipayInfo payAlipayInfo = payAlipayInfoMapper.findByFirst();
        Config config = new Config();
        config.protocol = payAlipayInfo.getProtocol();
        config.gatewayHost = payAlipayInfo.getGatewayHost();
        config.signType = payAlipayInfo.getSignType();
        config.appId = payAlipayInfo.getAppid();
        // 为避免私钥随源码泄露，推荐从文件中读取私钥字符串而不是写入源码中
        config.merchantPrivateKey = payAlipayInfo.getMerchant_private_key();
         config.alipayPublicKey = payAlipayInfo.getAlipay_public_key();
        //可设置异步通知接收服务地址（可选）
        config.notifyUrl = payAlipayInfo.getNotifyUrl();
        //可设置AES密钥，调用AES加解密相关接口时需要（可选）
        return config;
    }
}
